<?
session_start();

if($_SESSION["logged"]){

    include "sqlManager.php";
    $sqlManager = new sqlManager();

    if(isset($_POST[save])){
        if((empty($_POST[height]) || $_POST[height]<50 || $_POST[height]>300) && (!is_numeric($_POST[height]))){
            $report['height'] = "Neplatna vyska";
        }
        if(empty($_POST[weight]) || $_POST[weight]<3 || $_POST[weight] > 300 && (!is_numeric($_POST[weight]))){
            $report['weight'] = "Neplatna vaha.";
        }
        if(empty($report)){
            $userId = $sqlManager->getID($_SESSION["username"]);

            $height = $_POST[height]/100;
            $weight = $_POST[weight];
            $bmi = $weight / ($height*$height);

            $query = $sqlManager->saveBMI($userId, $height, $weight, $bmi);
            if(!$query){
                $report['sql'] = "Nepovedlo se ulozit BMI do databaze.";
            }else{
                $final = "Vas BMI je ".$bmi;
                unset($report);
            }
        }

    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <meta name="zajimaveeeeeee" content="Komodo Edit 8.5">
        <link rel="stylesheet" type="text/css" href="style.css">
        <title>BITWA - Fiala Dominik</title>

    </head>
    <body>
        <div id="hlavicka">
            <h1 align="center">BITWA</h1><br>
            <p>
                <?
                    echo $_SESSION[name]." (".$_SESSION[username].")". " <a href=\"prihlaseni.php\">Odhlasit</a>";
                ?>
            </p>
        </div>
        <div id="menu">
            <? include "menu.php" ?>
        </div>
        <div id="obsah">
            <form method="POST">

                <br/>
                <label for="height"> Vyska (cm) </label>
                <input type="text" id="height" name="height" value="<?php echo $_POST['height'];?>">

                <br/>
                <label for="weight"> Vaha (kg) </label>
                <input type="text" id="weight" name="weight" value="<?php echo $_POST['weight'];?>">

                <input type="submit" name="save" value="save">
            </form>
            <br><b>
            <?php
                foreach($report as $ch){
                    echo "<b>".$ch."</b><br>";
                }
                echo $final;
            ?>
            </b>
        </div>
        <div id="copyright">
            &copy fialadom
        </div>
        </body>
        </html>
<?
}else{
    ?>
        <meta http-equiv="refresh" content="0;url=http://databaze.fbmi.cvut.cz/fialadom/BITWA/CV5/prihlaseni.php">
    <?php
    }
?>